package com.yael.utils;

import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;

public class Datastore 
{
	private static final Logger logger = Logger.getLogger(Datastore.class.getName());

	@SuppressWarnings("unchecked")
	public static List<Object> query(PersistenceManager pm, Class<?> objClass, String filterExp, Map<String,Object> params)
	{ 
		List<Object> result;
		String declare = "";
		
		for(Map.Entry<String, Object> param : params.entrySet()){
			if(!declare.equals("")) declare += ", ";
			declare += param.getValue().getClass().getName()+" "+param.getKey();
		}
		javax.jdo.Query q = pm.newQuery(objClass, filterExp);
		q.declareParameters(declare);
		result = (List<Object>) q.executeWithMap(params);
		
		return result;
	 }
	
	 public static Object retriveByID(PersistenceManager pm, Class<?> objClass, Long id)
		{
			Object obj = pm.getObjectById(objClass, id);
			
			return obj;
		}

	public static void delete(PersistenceManager pm, Class<?> objClass, long id) {
		Object object = null;
		object = Datastore.retriveByID(pm, objClass, id);
		
		if(object!=null)
		{
			pm.deletePersistent(object);
			pm.close();
		}
	}
}


